/**
 * Created by ximh on 2017/4/27.
 */
//利用匿名函数将脚本包裹起来
(function(){
//自定义命名空间
var ced = {};

Date.prototype.Format = function(fmt) { //author: meizz
  var o = {
    "M+" : this.getMonth()+1,                 //月份
    "d+" : this.getDate(),                    //日
    "h+" : this.getHours(),                   //小时
    "m+" : this.getMinutes(),                 //分
    "s+" : this.getSeconds(),                 //秒
    "S"  : this.getMilliseconds(),             //毫秒
    "TT" : this.getHours() < 12 ? "AM" : "PM",
  };
  if (/(y+)/.test(fmt)){
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
      .substr(4 - RegExp.$1.length));
  }
  for ( var k in o){
    if (new RegExp("(" + k + ")").test(fmt)){
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
        : (("00" + o[k]).substr(("" + o[k]).length)));
    };
  }
  return fmt;
};

/**
 * yyyy-MM-dd HH:MM:SS
 * @param seperator1 “-”
 * @param seperator2 “:”
 * @param seperator3 空格
 * @returns {*}
 */
ced.getDate = function (seperator1, seperator2, seperator3) {
  var date = new Date();
  //var seperator1 = "-";
  //var seperator2 = ":";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    + seperator3 + date.getHours() + seperator2 + date.getMinutes()
    + seperator2 + date.getSeconds();
  return currentdate;
}

ced.trimStr = function (str) {
  return str.replace(/(^\s*)|(\s*$)/g, "");
}


//封包加解密
ced.encryptByDES = function (message, key) {
  var keyHex = CryptoJS.enc.Utf8.parse(key);
  var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
  return encrypted.toString();
}

/**
 * Decrypt ciphertext by DES in ECB mode and Pkcs7 padding scheme
 *
 * @param  {String} ciphertext(base64 string)
 * @param  {String} key
 * @return {String} plaintext
 *
 * @author Sun
 * @version 2013-5-15
 */
ced.decryptByDES = function decryptByDES(ciphertext, key) {
  var keyHex = CryptoJS.enc.Utf8.parse(key);

  // direct decrypt ciphertext
  var decrypted = CryptoJS.DES.decrypt({
    ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
  }, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });

  return decrypted.toString(CryptoJS.enc.Utf8);
}

//数组中过滤重复的数据
ced.unique = function (arr) {
  var result = [], isRepeated;
  for (var i = 0; i < arr.length; i++) {
    isRepeated = false;
    for (var j = 0; j < result.length; j++) {
      if (arr[i].id == result[j].id) {
        isRepeated = true;
        break;
      }
    }
    if (!isRepeated) {
      result.push(arr[i]);
    }
  }
  return result;
}

///////////add your function here///////////////////////////


////////////////////////////////////////////////////////////
//定义全局变量命令空间
window.CED = ced;
////////////////////////////////////////////////////////////
})();
